<template>
    <div>
        <!-- 面包屑导航区域 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 卡片视图区域 -->
        <el-card>
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入商品名称" v-model="queryInfo.productName" 
                    clearable @clear="getProductList">
                        <el-button slot="append" icon="el-icon-search" @click="getProductList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="goAddpage">添加商品</el-button>
                </el-col>
            </el-row>
            <!-- 数据表格区域 -->
            <el-table :data="productList" class="productTable" stripe height="450">
                <el-table-column type="index" label="序号" width="80px"></el-table-column>
                <el-table-column label="商品" width="260px">
                    <template slot-scope="scope">
                        <div class="productImage"><img :src="scope.row.mainImage.includes('http:')?scope.row.mainImage:$target + scope.row.mainImage" alt="" width="100px" height="100px"/></div>
                        <div class="productName">{{ scope.row.productName }}</div>
                    </template>
                </el-table-column>
                <el-table-column prop="productTitle" label="商品标题" width="120px" show-overflow-tooltip></el-table-column>
                <el-table-column prop="productDescribe" label="商品描述" width="120px" show-overflow-tooltip></el-table-column>
                <el-table-column label="售价(￥)" width="120px">
                    <template slot-scope="scope">
                        <span v-if="scope.row.promotionPrice != null && scope.row.marketPrice != null" class="price">
                            <span
                                v-show="scope.row.marketPrice != scope.row.promotionPrice"
                                class="del"
                            >{{scope.row.marketPrice}}</span>
                            <span>{{ scope.row.promotionPrice }}</span>
                        </span>
                        <span class="price" v-else>
                            {{ scope.row.marketPrice }}
                        </span>
                    </template>
                </el-table-column>
                <el-table-column prop="inventory" label="库存" width="100px"></el-table-column>
                <el-table-column prop="salesVolume" label="销量" width="100px"></el-table-column>
                <el-table-column label="商品状态" width="100px">
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.productState === 1? 'danger': ''">{{ scope.row.productState | productStateFilter }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="是否推荐" width="100px">
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.isRecommend === 0? 'info': 'success'">{{ scope.row.isRecommend | isRecommendFilter }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="所属分类" width="100px">
                    <template slot-scope="scope">
                        {{ scope.row.productType.productTypeName }}
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" width="160px">
                    <template slot-scope="scope">
                        {{scope.row.createTime}}
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作" width="240px">
                    <template slot-scope="scope">
                        <el-tooltip effect="dark" content="修改" placement="top" :enterable="false">
                            <el-button type="primary" size="mini" icon="el-icon-edit" @click="goUpdatePage(scope.row.productId)"></el-button>
                        </el-tooltip>
                        <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
                            <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeById(scope.row.productId)"></el-button>
                        </el-tooltip>
                        <el-tooltip effect="dark" :content="scope.row.productState === 1? '上架' : '下架'" placement="top" :enterable="false">
                            <el-button :type="scope.row.productState === 1? 'success' : 'warning'" size="mini" :icon="scope.row.productState === 1? 'el-icon-sell' : 'el-icon-sold-out'"
                            @click="changeSellState(scope.row.productId)"></el-button>
                        </el-tooltip>
                        <el-tooltip effect="dark" :content="scope.row.isRecommend === 0? '开启推荐': '关闭推荐'" placement="top" :enterable="false">
                            <el-button :type="scope.row.isRecommend === 0? 'info' : 'success'" size="mini" icon="el-icon-star-off" @click="recommend(scope.row.productId)"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页区域 -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.currentPage"
                :page-sizes="[5, 10, 20, 30]" :page-size="queryInfo.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
export default {
    data() {
        return {
            // 获取商品列表的参数对象
            queryInfo: {
                productName: '',
                //当前页数
                currentPage: 1,
                //当前每页显示多少条数据
                pageSize: 10
            },
            //商品列表
            productList: [],
            //总数据条数
            total: 0
        }
    },
    activated() {
        this.getProductList();
    },
    // created (){
        
    // },
    filters: {
        productStateFilter(value) {
            let productStateStr = '';
            switch(value){
                case 0:
                    productStateStr = '正常';
                    break;
                case 1:
                    productStateStr = '已下架';
                    break;
            }
            return productStateStr;
        },
        isRecommendFilter(value) {
            let isRecommendStr = '';
            switch(value){
                case 0:
                    isRecommendStr = '已关闭';
                    break;
                case 1:
                    isRecommendStr = '已开启';
                    break;
            }
            return isRecommendStr;
        }
    },
    methods: {
        //根据分页获取对应的商品列表
        getProductList(){
            this.$axios
            .get('/product', { params: this.queryInfo })
            .then(res => {
                const data = res.data;
                if(data.code !== '001') return this.$notifyError("数据获取异常");
                this.productList = data.list;
                this.total = data.totalRecords;
            })
            .catch(err => {
                console.log(err);
            });
        },
        handleSizeChange(newSize) {
            this.queryInfo.pageSize = newSize;
            this.getProductList();
        },
        handleCurrentChange(newPage) {
            this.queryInfo.currentPage = newPage;
            this.getProductList();
        },
        removeById(productId){
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //发起删除请求
                this.$axios
                .delete('/product/' + productId)
                .then(res => {
                    const data = res.data;
                    if(data.code !== '001') return this.$notifyError(data.msg);
                    this.$notifySucceed(data.msg);
                    this.getProductList();
                })
                .catch(err => {
                    console.log(err);
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        goAddpage() {
            this.$router.push('/product/add')
        },
        goUpdatePage(productId){
            this.$router.push({ path: '/product/update', query: { productId: productId } })
        },
        //开启/关闭推荐
        recommend(productId){
            const data = {
                productId : productId
            }
            //发起删除请求
            this.$axios
            .put('/product/recommend', data)
            .then(res => {
                const data = res.data;
                if(data.code !== '001') return this.$notifyError(data.msg);
                this.$notifySucceed(data.msg);
                this.getProductList();
            })
            .catch(err => {
                console.log(err);
            })
        },
        //商品销售状态：上架/下架
        changeSellState(productId) {
            const data = {
                productId : productId
            }
            //发起删除请求
            this.$axios
            .put('/product/changeSellState', data)
            .then(res => {
                const data = res.data;
                if(data.code !== '001') return this.$notifyError(data.msg);
                this.$notifySucceed(data.msg);
                this.getProductList();
            })
            .catch(err => {
                console.log(err);
            })
        }
    }
}
</script>

<style lang="less" scoped>
.productTable {
    // display: table;
    box-sizing: border-box;
    // text-align: center;
}
.productTable  .productImage,.productName {
    display: table-cell;
	vertical-align: middle;
    padding: 5px;
}
.productTable  .productImage {
    width: 100px;
    height: 100px;
}
.productTable  .productName {
    width: 120px;
    height: 120px;
    // margin-left: 5px;
    color: #000;
}
.productTable .price {
    font-size: 14px;
    color: #ff6700;
}
.productTable .price>span {
    padding: 5px;
}
.productTable .del {
    text-decoration: line-through;
    color: #b0b0b0;
}
// .productTable .market-price,.promotion-price{
//     padding: 5px;
// }
// .productTable .market-price {
//     text-decoration: line-through;
//     color: #b0b0b0;
// }
// .productTable .promotion-price {
//     color: #ff6700;
// }
</style>